package com.zzj.portalwebsite.Controller;

import com.alibaba.fastjson.JSON;
import com.zzj.portalwebsite.entity.FeedBack;
import com.zzj.portalwebsite.factory.Factory;
import com.zzj.portalwebsite.utlis.CommonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/message/query")
public class MessageController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String pagenumStr = request.getParameter("pagenum");
        String pagesizeStr = request.getParameter("pagesize");
        System.out.println("Received pagenum: " + pagenumStr + ", pagesize: " + pagesizeStr);
        if (pagenumStr == null) {
            pagenumStr = "1";
        }
        if (pagesizeStr == null) {
            pagesizeStr = "5"; // 蛇者每页显示5条记录
        }
        int pageNum = Integer.parseInt(pagenumStr);
        int pageSize = Integer.parseInt(pagesizeStr);
        System.out.println(pageSize);

        try {
            List<FeedBack> feedBack = Factory.getFeedBackServiceInstance().getFeedBack(pageNum, pageSize);
            if (feedBack != null) {
                response.getWriter().write(JSON.toJSONString(CommonResult.success(feedBack)));
                for (FeedBack back : feedBack) {
                    System.out.println(back.getCreate_time());
                }
            } else {
                response.getWriter().write("获取失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}